package com.example.beauty.contact;

import android.content.Intent;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beauty.db.gen.Note;
import com.example.beauty.db.gen.NoteDao;

import java.text.SimpleDateFormat;
import java.util.Date;


public class EditContact extends AppCompatActivity {

    private NoteDao edit_noteDao;
    private GreenDaoManager edit_initDao;
    private Note note;
    Bundle bundle;  /*封装Note对象数据的容器*/
    String nameStr,phoneStr; /*接收ContactMain传递过来的note对象的名字，电话号码*/
    Long id;/*接收ContactMain传递过来的note对象的id*/
    private EditText name_et,phone_et;
    private Intent intent;
    ImageButton callImgBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_contact);

        /*初始化GreenDao框架的数据库,数据库名为Info*/
        edit_initDao.InitDao(EditContact.this,"Info.db");
        edit_noteDao = GreenDaoManager.getDaoSession().getNoteDao();

        name_et = (EditText) findViewById(R.id.name_EditText);
        phone_et = (EditText) findViewById(R.id.phone_EditText);

        /*获取ContactMain点击的Note对象的数据*/
        note = new Note();
        bundle = getIntent().getExtras();
        nameStr = bundle.getString("datanoteName");
        phoneStr = bundle.getString("datanotePhone");
        /*如果点击的是add菜单，可输入*/
        if ("".equals(nameStr)){
            /*把文本框设置为能输入*/
            name_et.setEnabled(true);
            phone_et.setEnabled(true);
        }
        else
        {
            /*把获取的名字放入编辑框中*/
            id = bundle.getLong("datanoteId");
            name_et.setText(nameStr);
            phone_et.setText(phoneStr);
            /*把文本框设置为不能输入*/
            name_et.setEnabled(false);
            phone_et.setEnabled(false);
        }
        Log.i("EditContact","tht note name in Edit is:"+nameStr);
        Log.i("EditContact","tht note phone in Edit is:"+phoneStr);
        Log.i("EditContact","tht note id in Edit is:"+id);

        /*为通话图片按钮注册监听器*/
        callImgBtn = (ImageButton)findViewById(R.id.imageButton_call);
        callImgBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                /*此处添加callImgBtn响应事件*/
                /*获取电话号码文本框的值,为空则提醒*/
                phoneStr = phone_et.getText().toString();
                if(phoneStr.isEmpty())
                {
                    Toast.makeText(EditContact.this,"还未输入号码！请输入。",Toast.LENGTH_SHORT).show();
                    return;
                }
                else
                {
                    ToTelView(phoneStr);
                }
                /*Toast.makeText(EditContact.this,"Clicked callImgBtn!",
                Toast.LENGTH_SHORT).show();*/
                /*结束该活动*/
                EditContact.this.finish();
            }
        });

    }

    /*增加菜单，并显示菜单*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        /*inflate()有两个参数，第一个指通过那个资源文件创建菜单，
        * 第二个指菜单项添加到哪个Menu对象*/
        getMenuInflater().inflate(R.menu.contact_edit_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    /*添加菜单项相应事件*/
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.save_item:
                /*此处添加Save菜单项相应事件*/
                /*插入数据*/
                saveNote();
                /*清空EditText*/
                name_et.setText("");
                phone_et.setText("");
                /*Toast.makeText(this,"clicked Save Menu and insert successful!",
                Toast.LENGTH_SHORT).show();*/
                /*返回主界面*/
                Intent intentAdd = new Intent(EditContact.this,ContactMain.class);
                startActivity(intentAdd);
                /*结束该活动*/
                EditContact.this.finish();
                break;
            case R.id.cancel_item:
                /*此处添加Cancel菜单项相应事件*/
                /*清空EditText*/
                name_et.setText("");
                phone_et.setText("");
                /*Toast.makeText(this,"clicked Cancel Menu!",Toast.LENGTH_SHORT).show();*/
                /*返回主界面*/
                Intent intentCancel = new Intent(EditContact.this,ContactMain.class);
                startActivity(intentCancel);
                /*结束该活动*/
                EditContact.this.finish();
                break;
            case R.id.remove_item:
                /*此处添加Remove菜单项相应事件*/
                if(id!=null)
                {
                    removeNote(id);
                }
                else
                {
                    Toast.makeText(this,"尚未保存联系人，不能删除！",Toast.LENGTH_SHORT).show();
                }
                /*Toast.makeText(this,"clicked Remove Menu!",Toast.LENGTH_SHORT).show();*/
                Intent intentRemove = new Intent(EditContact.this,ContactMain.class);
                startActivity(intentRemove);
                /*结束该活动*/
                EditContact.this.finish();
                break;
            case R.id.edit_item:
                /*此处添加Edit菜单项相应事件*/
                name_et.setEnabled(true);
                phone_et.setEnabled(true);
                saveNote();
        }
        return super.onOptionsItemSelected(item);
    }

    /*配合ContactMain采用SingleTask模式，销毁活动*/
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Log.d("EditContact","OnDestroy!");
    }

    /*调用拨号界面函数*/
    public void ToTelView(String phone){
        intent = new Intent(Intent.ACTION_DIAL);

//        intent.setData(Uri.parse("tel:"+phone));
        intent.setData(Uri.parse("tel:"+phone));
        Log.i("EditContact","ToTalView the telnum is:"+phone);
        startActivity(intent);
    }


    /*作用：保存数据，判断插入操作还是更新操作，
    * 判断过后执行对应操作
    */
    private void saveNote()
    {
        String name = name_et.getText().toString();
        String phone = phone_et.getText().toString();
        if(name.isEmpty())
        {
            Toast.makeText(EditContact.this,"未输入名字！不能保存！",Toast.LENGTH_SHORT).show();
            return;
        }
        else
        {
            /*获取系统时间,小写的mm表示的是分钟*/
            Date date = new Date();
            String dateStr =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            Log.i("EditContact","Add note Date is:"+dateStr);
            note = new Note();
            note.setDate(dateStr);
            note.setName(name);
            note.setPhone(phone);
        /*判断插入数据还是更新数据，更新数据时id不为空且与
        * bundle的datanoteID值相等*/
            if(id!=null){
                note.setId(id);
            /*更新数据*/
                edit_noteDao.update(note);
            }
            else
            {
            /*插入数据*/
                edit_noteDao.insert(note);
            }
        }
    }

    private void removeNote(Long id)
    {
        /*获取所点击的ItemId*/
        edit_noteDao.deleteByKey(id);
        Log.i("EditContact","Delete by key on id:"+id);
    }
}
